package com.yiren.dbaa.module.practice_set.map.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.dbaa.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lwb
 * @date : 2022/11/29 10:18
 * @description: 共享地图适配器
 */

public class ShareMapAdapter extends BaseAdapter {

    private Context context;
    private List<MapEntity> list;

    public ShareMapAdapter(Context context, List<MapEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.item_share_line, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        MapEntity map = list.get(position);
        holder.tvName.setText(map.getName());
        holder.tvPoints.setVisibility(View.GONE);
        holder.vBar1.setVisibility(View.GONE);
        if (map.getMapType() == 1) {
            holder.tvType.setText("考试路段");
            holder.vBar3.setVisibility(View.GONE);
            holder.tvLocation.setVisibility(View.GONE);
        } else {
            holder.tvType.setText("训练路段");
            holder.tvLocation.setText(map.getAddress());
        }
        holder.tvUser.setText(map.getUserId() + "");

        if (map.isDownLoad()) {
            holder.tvDownload.setBackground(context.getResources().getDrawable(R.drawable.btn_gray_select_shape));
            holder.tvDownload.setText("已下载");
        }else {
            holder.tvDownload.setText("  下载  ");
        }
        holder.tvPreview.setVisibility(View.GONE);

        holder.tvPreview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnPreviewClickListener != null) {
                    mOnPreviewClickListener.onPreviewClick(position);
                }
            }
        });
        holder.tvDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnDownloadClickListener != null && !map.isDownLoad()) {
                    mOnDownloadClickListener.onDownloadClick(position);
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_points)
        TextView tvPoints;
        @BindView(R.id.v_bar1)
        View vBar1;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_user)
        TextView tvUser;
        @BindView(R.id.v_bar3)
        View vBar3;
        @BindView(R.id.tv_location)
        TextView tvLocation;
        @BindView(R.id.tv_preview)
        TextView tvPreview;
        @BindView(R.id.tv_download)
        TextView tvDownload;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //条目预览回调
    public interface OnPreviewClickListener {
        void onPreviewClick(int position);
    }

    private OnPreviewClickListener mOnPreviewClickListener;

    public void setOnPreviewClickListener(OnPreviewClickListener onPreviewClickListener) {
        mOnPreviewClickListener = onPreviewClickListener;
    }

    //下载点击回调
    public interface OnDownloadClickListener {
        void onDownloadClick(int position);
    }

    private OnDownloadClickListener mOnDownloadClickListener;

    public void setOnDownloadClickListener(OnDownloadClickListener onDownloadClickListener) {
        mOnDownloadClickListener = onDownloadClickListener;
    }

}